﻿Imports NUnit.Framework
Imports ValidationFramework

<TestFixture()> _
Public Class ValidatableRuleAttributeExample


    ' Nested Types
    Public Class Address
        Inherits ValidatableBase
        ' Properties
        <RequiredStringRule()> _
        Public Property Street() As String
            Get
                Return _street
            End Get
            Set(ByVal value As String)
                _street = value
            End Set
        End Property


        ' Fields
        Private _street As String
    End Class

    Public Class Person
        Inherits ValidatableBase
        ' Properties
        <ValidatableRule(UseMemberErrorMessages:=True), RequiredObjectRule()> _
        Public Property Address() As Address
            Get
                Return _address
            End Get
            Set(ByVal value As Address)
                _address = value
            End Set
        End Property


        ' Fields
        Private _address As Address
    End Class


    ' Methods
    <Test()> _
    Public Sub Run()
        Dim person As New Person
        ' Person is invalid because Address is null.
        Assert.IsFalse(person.IsValid)

        person.Address = New Address
        ' Person is invalid because Address has no street.
        Assert.IsFalse(person.IsValid)
        ' The error message from address is only passed up because 
        ' UseMemberErrorMessages is set to true on the ValidatableRule
        Assert.AreEqual("The property 'Street' is required.", person.ErrorMessages.Item(0))

        ' Set the street.
        person.Address.Street = "1 puddle lane"
        ' Person is Valid.
        Assert.IsTrue(person.IsValid)
    End Sub
End Class


